<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PPLayoutControl.php 329 2008-12-14 06:30:02Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Common.Data.PortalConfigRecord');
class PPLayoutControl extends TWebControl
{

	public function getControlSpacing()
	{
		return $this->getViewState('ControlSpacing', '');
	}

	public function setControlSpacing($value)
	{
		return $this->setViewState('ControlSpacing', $value);
	}

	public function getTagName()
	{
		return 'div';
	}

	public function onInit($p)
	{
		$this->Application->setLayoutAreas($this->Id);
		parent::onInit($p);
	}

	public function render($writer)
	{
		$am = $this->Application->getModule('auth');
		$canedit = $am->CanEdit();

		// Get the property configuration
		$name	= $this->Service->RequestedPagePath . '_' . $this->Id;
		try
		{
			if($this->Application->Parameters['RunMode'] !== 'Install')
			{
				$lConfig	= PortalConfigRecord::finder()->findByname($name);
				if($lConfig)
				{
					$lProperties	= Prado::unserialize($lConfig->value);
					if(is_array($lProperties) && count($lProperties) > 0)
					{
						foreach($lProperties as $prop_key => $prop_value)
						{
							if($prop_key == 'style' || $prop_key == 'class')
							{
								if($prop_key === 'class')
								{
									if(substr($prop_value,0,1) == '.')
										$prop_value = substr($prop_value,1,999);
								}
								$writer->addAttribute($prop_key, $prop_value);
							}
						}
					}
				}
			}
		}
		catch (Exception $e)
		{
			PortalUtil::fatalError($e->getMessage());
		}

		if($this->Page->EditMode == 1 && $canedit)
		{
			$id = "adm_" . uniqid();
			$sb = "scrollbar-3dlight-color: #F0F0EE;scrollbar-arrow-color: #676662;scrollbar-base-color: #F0F0EE;scrollbar-darkshadow-color: #DDDDDD;scrollbar-face-color: #E0E0DD;scrollbar-highlight-color: #F0F0EE;scrollbar-shadow-color: #F0F0EE;scrollbar-track-color: #F5F5F5;";
			$writer->write("<div id='$id' style='border: 1px dotted red; margin:0px;overflow: auto;$sb'><div style='margin:0px;padding:0px;'>");

			if($this->ID != '')
			{
				// Add the 'Add' images
				$imgpath = $this->Page->getTheme()->getBaseUrl();
				$link_prop = $this->Service->constructUrl('System.LayoutProperty',array('PageMode' => $this->Page->EditMode,'pid' => $this->Service->RequestedPagePath,'cid' => $this->ID));
				$link = $this->Service->constructUrl('System.AddPortlet',array('PageMode' => $this->Page->EditMode,'pid' => $this->Service->RequestedPagePath,'cid' => $this->ID));
				$writer->write("<table width='100%' cellspacing='0' cellpadding='5' border='0' style='border: 1px dotted black;'><tr><td width='80%'><strong>" . $this->Id . "</strong></td>");
				$writer->write("<td width='20%'style='text-align: right;'><a href='$link_prop'><img src='". $imgpath . "/images/edit/application_form.png' alt='" . Prado::localize("Properties") . "' border='0' /></a>&nbsp;<a href='$link'><img src='". $imgpath . "/images/edit/application_add.png' alt='" . Prado::localize("Add element to area") . "' border='0' /></a></td></tr></table>");
			}
			else
			{
				$writer->write("<strong style='font-size: 8px;'>" . Prado::localize("Only controls with id can be modified") . "</strong>");
			}
			parent::render($writer);
			$writer->write("</div></div>");
		}
		else
		{
			parent::render($writer);
		}
	}
}

?>
